import * as Apis from "../../apis/Api";
import { observable, action } from 'mobx';
import SynergyStore from './SynergyStore';

class ActualBillStore{
    @observable refId=-1;
    @observable payType="1";
    @observable payTypeName="收";
    @observable bills = [];
    @observable total = {};
    @observable rights = {};
    @observable canAdd = false;
    @observable modeInfo = {};
    @observable rightMeus = [];

    @action 
    componentWillUnmount = ()=>{
        this.bills = [];
        this.total = {};
        this.rights = {};
        this.canAdd = false;
    }

    @action
    init = (refId=-1) => {
        this.refId = refId;
        this.payTypeName = this.payType==="1"?"收":"付";

        const requestParams = {contractId:refId,payType:this.payType};
        Apis.getActualBill(requestParams).then(data => {
            this.total = {proportion:data.proportion,payTotal:data.payTotal};
            this.bills = data.datas;
            const ids = data.datas.map(item=>{
                return item.id;
            });

            Apis.getModeInfo().then(data1 => {
                this.modeInfo = this.payType==="1"?data1.incom:data1.pay;
                // if(`${ids}`!==""){
                    const p = {billid:`${ids}`,...(this.payType==="1"?data1.incom:data1.pay)};
                    Apis.getRightInfo(p).then(data2 => {
                        this.rights = data2.datas;
                        this.canAdd = data2.datas.Add||true;

                        // this.setRightMenus(this.getRightMenu());

                        this.getRightMenu();
                    });
                // }
            });
        });
        

    }

    @action
    setPayType = (payType="1")=>{
        this.payType = payType;
    }

    @action
    getRightMenu = ()=>{
        const fieldId = typeof(this.modeInfo.cons_name)=='string'?this.modeInfo.cons_name.substring(this.modeInfo.cons_name.indexOf('_')+1,this.modeInfo.cons_name.length):'';
        const fieldStr = `field${fieldId}`;
        let params = {};
        params["type"] = 1;
        params["modeId"] = this.modeInfo.modeId;
        params["formId"] = this.modeInfo.formId;
        params["customid"] = this.modeInfo.customid;
        params[fieldStr] = this.refId;
        const arr = [];
        if(this.canAdd){
            arr.push({
                icon: <i className={"icon-coms-New-Flow"} />,
                content: "新建",
                key: '1',
                // onClick: key => window.openFullWindowHaveBar(`#/main/cube/card?type=1&modeId=${this.modeInfo.modeId}&formId=${this.modeInfo.formId}&field${fieldId}=${this.refId}`),
                onClick:key =>window._contractSynergy.loadSynergyDialog(params,'actualBillStore')
            });
        }

        this.rightMeus = arr;
        // return arr;

    }

}

export default ActualBillStore;